import { post, apiPaths } from '../../utils/request.js'

Page({
  data: {
    username: 'admin123',
    password: '123456',
    rememberMe: false
  },

  onLoad() {
    // 检查是否记住登录信息
    const rememberMe = wx.getStorageSync('rememberMe') || false;
    if (rememberMe) {
      const username = wx.getStorageSync('username') || '';
      const password = wx.getStorageSync('password') || '';
      
      this.setData({
        username: username,
        password: password,
        rememberMe: rememberMe
      });
    }
  },

  onUsernameInput(e) {
    this.setData({
      username: e.detail.value
    });
  },

  onPasswordInput(e) {
    this.setData({
      password: e.detail.value
    });
  },

  onRememberMeChange(e) {
    this.setData({
      rememberMe: e.detail.value
    });
  },

  onLogin() {
    if (!this.data.username) {
      wx.showToast({
        title: '请输入用户名',
        icon: 'none'
      });
      return;
    }
    
    if (!this.data.password) {
      wx.showToast({
        title: '请输入密码',
        icon: 'none'
      });
      return;
    }
    
    // 调用登录接口
    post(apiPaths.login, {
      username: this.data.username,
      password: this.data.password
    }).then(res => {
      if (res.code === 200) {
        wx.showToast({
          title: '登录成功',
          icon: 'success'
        });
        
        // 保存token
        if (res.data && res.data.token) {
          wx.setStorageSync('token', res.data.token);
          console.log('Token saved:', res.data.token);
        } else {
          console.warn('No token received from login response');
        }
        
        // 保存用户信息
        if (res.data && res.data.user) {
          wx.setStorageSync('userInfo', res.data.user);
          console.log('User info saved:', res.data.user);
        } else {
          console.warn('No user info received from login response');
        }
        
        // 保存登录信息
        if (this.data.rememberMe) {
          wx.setStorageSync('username', this.data.username);
          wx.setStorageSync('password', this.data.password);
          wx.setStorageSync('rememberMe', true);
        } else {
          wx.removeStorageSync('username');
          wx.removeStorageSync('password');
          wx.setStorageSync('rememberMe', false);
        }
        
        // 保存登录状态
        wx.setStorageSync('isLogin', true);
        
        // 直接跳转到首页
        setTimeout(() => {
          wx.switchTab({
            url: '/pages/index/index'
          });
        }, 1500);
      } else {
        wx.showToast({
          title: res.message || '登录失败',
          icon: 'none'
        });
      }
    }).catch(err => {
      wx.showToast({
        title: err.message || '登录失败',
        icon: 'none'
      });
    });
  },

  onWechatLogin() {
    wx.showToast({
      title: '微信登录功能开发中',
      icon: 'none'
    });
    
    // 微信登录也直接跳转到首页
    setTimeout(() => {
      wx.switchTab({
        url: '/pages/index/index'
      });
    }, 1500);
  },

  onForgotPassword() {
    wx.showToast({
      title: '忘记密码功能开发中',
      icon: 'none'
    });
  },

  onSignup() {
    wx.navigateTo({
      url: '/pages/register/register'
    });
  }
});